package com.ml.util;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.ml.dto.OrderParam;
import com.ml.exception.ExchangeAssert;
import com.ml.exception.ExchangeError;
import com.ml.message.OrderDTO;
import com.ml.message.OrderSuccessVO;
import com.ml.model.Symbol;

/**
 * 
 * @author Virgil
 * @Date 2020年8月21日
 * @Describe
 *           <p>
 *
 *           </p>
 */
public class DataUtil {

	public static OrderSuccessVO checkParam(OrderParam param, Symbol symbol) {
		OrderSuccessVO order = new OrderSuccessVO();
		// 生成订单ID
		order.setId(SnowflakeIdWorker.generateId());
		// 买卖单
		order.setIfBid(param.isBuy());
		// 交易类型
		order.setOrderType(param.getOrderType());
		// 数量
		order.setNum(param.getNum().setScale(symbol.getNumPrecision()));
		// 价格
		order.setPrice(param.getPrice().setScale(symbol.getPricePrecision()));
		// 未成交数量
		order.setNoDealNum(order.getNum());
		// 已成交数量
		order.setDealNum(BigDecimal.ZERO.setScale(symbol.getNumPrecision()));
		// 获取交易对
		order.setSymbol(symbol.getSymbol());
		order.setSymbolId(symbol.getId());
		// 精度
		order.setAmountPrecision(symbol.getAmountPrecision());
		order.setPricePrecision(symbol.getPricePrecision());
		order.setNumPrecision(symbol.getNumPrecision());

		if (order.getOrderType() == 1) {
			// 限价单
			if (order.getNum().compareTo(symbol.getLimitOrderMaxNum()) > 0) {
				ExchangeAssert.createException(ExchangeError.Arg, "数量必须小于等于" + symbol.getLimitOrderMaxNum());
			}
			if (order.getNum().compareTo(symbol.getLimitOrderMinNum()) < 0) {
				ExchangeAssert.createException(ExchangeError.Arg, "数量必须大于等于" + symbol.getLimitOrderMinNum());
			}
			order.setAmount(order.getNum().multiply(order.getPrice()).setScale(symbol.getAmountPrecision()));
			if (order.getAmount().compareTo(symbol.getOrderMaxAmount()) > 0) {
				ExchangeAssert.createException(ExchangeError.Arg, "交易额必须小于等于" + symbol.getLimitOrderMaxNum());
			}
			if (order.getAmount().compareTo(symbol.getOrderMinAmount()) < 0) {
				ExchangeAssert.createException(ExchangeError.Arg, "交易额必须大于等于" + symbol.getLimitOrderMinNum());
			}

		} else if (order.getOrderType() == 2) {
			if (param.isBuy()) {
				order.setNum(BigDecimal.ZERO);
				order.setAmount(param.getNum().setScale(symbol.getAmountPrecision()));
				order.setNoDealNum(BigDecimal.ZERO);
				if (order.getAmount().compareTo(symbol.getOrderMaxAmount()) > 0) {
					ExchangeAssert.createException(ExchangeError.Arg, "交易额必须小于等于" + symbol.getLimitOrderMaxNum());
				}
				if (order.getAmount().compareTo(symbol.getOrderMinAmount()) < 0) {
					ExchangeAssert.createException(ExchangeError.Arg, "交易额必须大于等于" + symbol.getLimitOrderMinNum());
				}
			} else {
				if (order.getNum().compareTo(symbol.getLimitOrderMaxNum()) > 0) {
					ExchangeAssert.createException(ExchangeError.Arg, "数量必须小于等于" + symbol.getSellMarketMaxOrderNum());
				}
				if (order.getNum().compareTo(symbol.getLimitOrderMinNum()) < 0) {
					ExchangeAssert.createException(ExchangeError.Arg, "数量必须大于等于" + symbol.getSellMarketMinOrderNum());
				}
			}
		}
		order.setNoDealAmount(order.getAmount());
		order.setDealAmount(BigDecimal.ZERO.setScale(symbol.getAmountPrecision()));
		order.setState(0);
		order.setCreateTime(new Date());

		return order;
	}

	public static OrderDTO toOrderMessage(OrderSuccessVO order) {
		OrderDTO dto = new OrderDTO();
		dto.setId(order.getId());
		dto.setUid(order.getUid());
		dto.setAccountId(order.getAccountId());
		dto.setIfBid(order.isIfBid());
		dto.setSymbolId(order.getSymbolId());
		dto.setOrderType(order.getOrderType());
		dto.setNum(MathUtils.BigDecimalToLong(order.getNum(), order.getNumPrecision()));
		dto.setPrice(MathUtils.BigDecimalToLong(order.getPrice(), order.getPricePrecision()));
		dto.setNoDealNum(dto.getNum());
		if (order.getOrderType() == 1) {
			dto.setAmount(MathUtils.BigDecimalToLong(order.getAmount(), order.getAmountPrecision()));
		} else if (order.getOrderType() == 2 && dto.isIfBid()) {
			dto.setNum(0);
			dto.setAmount(MathUtils.BigDecimalToLong(order.getNum(), order.getAmountPrecision()));
			dto.setNoDealNum(0);
		}
		dto.setNoDealAmount(dto.getAmount());
		dto.setDealAmount(0);
		dto.setState(order.getState());
		dto.setCreateTime(new Date());
		return dto;
	}

	/**
	 * @Description 用于获取url参数
	 * @Author Mr.D
	 * @Date 2020/7/29 13:43
	 * @Param
	 * @Return
	 * @Exception
	 *
	 */
	public static Map<String, String> getQueryMap(String queryStr) {
		Map<String, String> queryMap = new HashMap<>();
		if (!StringUtils.isEmpty(queryStr)) {
			String[] queryParam = queryStr.split("&");
			Arrays.stream(queryParam).forEach(s -> {
				String[] kv = s.split("=", 2);
				String value = kv.length == 2 ? kv[1] : "";
				queryMap.put(kv[0], value);
			});
		}
		return queryMap;
	}
}
